-
Notifications
You must be signed in to change notification settings - Fork 441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
contrib/database/sql: Close DB Stats goroutine on db.Close() #3025
base: main
Are you sure you want to change the base?
Conversation
Datadog ReportBranch report: ❌ 1 Failed (0 Known Flaky), 5014 Passed, 71 Skipped, 2m 44.91s Total Time ❌ Failed Tests (1)
|
0692055
to
37b52f9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I left some small nits.
}) | ||
} | ||
|
||
func GetStopChan() chan struct{} { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func GetStopChan() chan struct{} { | |
func StopChan() chan struct{} { |
This would be a better name.
mu sync.Mutex | ||
) | ||
|
||
func InitStopChan() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func InitStopChan() { | |
func Init() { |
Just in case this package evolves and needs to initialize more things beyond the stop chan.
Datadog ReportBranch report: ✅ 0 Failed, 5208 Passed, 72 Skipped, 2m 45.92s Total Time |
What does this PR do?
Introduces a stop condition for the
pollDBStats
goroutine when db.Close() has been invoked by the user.Motivation
Protecting against runaway goroutines.
Details
Background
To register a sql driver for instrumentation, a user must call sqltrace.Open, which passes our
tracedConnector
into the OpenDB function. ThistracedConnector
is used as thec driver.Connector
argument to database/sql's OpenDB function, which is set on the DB.connector field.When
db.Close()
is called by the user, the database/sql code checks whether theDB.connector
satisfies theio.Closer
interface and, if so, callsconnector.Close()
.Therefore, to identify when a user has called
db.Close()
, we can implement (and control) atracedConnector.Close()
method.Implementation
A new
tracedConnector.dbClose
channel is closed whentracedConnector.Close()
is invoked. ThepollDBStats
goroutine returns (stops) when there is activity on thedbClose
channel.In this way, we've tied the
pollDBStats
goroutine to the lifecycle of the db.Next Steps
Once this approach is approved, we will do the same thing for the contrib/jackc/pgx
pollPoolStats
goroutine.Reviewer's Checklist
v2-dev
branch and reviewed by @DataDog/apm-go.Unsure? Have a question? Request a review!